//
//  HMComposePictureViewCell.swift
//  WB25
//
//  Created by HM on 2017/7/8.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMComposePictureViewCell: UICollectionViewCell {
    
    // 删除指定图片
    var closure:(()->())?
    
    // 定义一个属性(类型 UIImage)
    var image: UIImage?{
        didSet{
            // 正常的图片
            if let i = image {
                bgImageView.image = i
                bgImageView.highlightedImage = nil
            }else{
                // 加号图片
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            }
            
            // 是否显示删除按钮
            deleteButton.isHidden = (image == nil)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 监听事件
    @objc private func deleteClick(){
        print("删除按钮点击")
        closure?()
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        
        // 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        // 添加约束
        bgImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        deleteButton.snp.makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
    }
    
    //MARK: - 懒加载加载
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 监听事件
        button.addTarget(self, action: #selector(deleteClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        return button
    }()
}
